package org.robot.work.into.oalib20190211;

import java.io.File;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.IntStream;

import org.robot.Spider;
import org.robot.component.Attachment;
import org.robot.component.Page;
import org.robot.dao.IssueDao;
import org.robot.dao.JournalDao;
import org.robot.dao.PaperDao;
import org.robot.pojo.Issue;
import org.robot.pojo.Journal;
import org.robot.pojo.PaperInfo;
import org.robot.utils.ParseUtil;
import org.robot.utils.XmlUtil;

/**
 * 完成
 * 放射学实践
 * @author pdz
 */
public class Fsxsj extends Spider {
	private static final Long TASKID = 1714L;
	private static List<Attachment> list = Collections.synchronizedList(new LinkedList<>());

	@Override
	public void parse(Page page) throws Exception {
		if (page.isSeed()) {
			parseJournal(page);
			IntStream.rangeClosed(2015, 2018).forEach(year -> IntStream.rangeClosed(1, 12)
					.forEach(quarter -> {
						String qua = new DecimalFormat("00").format(quarter);
						Map<String, Object> map = page.createMap("year", year);
						map.put("qua", qua);
						page.addNext("http://www.fsxsj.net/ch/reader/issue_list.aspx?year_id="+year+"&quarter_id="+qua+"&Submit=", map, "issue");
					}));
		} else if (page.typeEquals("issue")) {
			parseIssue(page);
		} else if (page.typeEquals("paper")) {
			parsePaper(page);
		}
	}
	
	private void parsePaper(Page page) {
		PaperInfo paperInfo = ParseUtil.parsePaperInfo1(page, TASKID);
		PaperDao.addPaperInfo(paperInfo);
		
	}

	private void parseIssue(Page page) {
		String pubYear = page.get("year").toString();
		String quarter = page.get("qua").toString();
		String issueName = pubYear + "年第" + quarter + "期";
		page.put("pubYear", pubYear);
		page.put("issueName", issueName);
		
		Long journalid = JournalDao.selectPidByWebsite(Journal.class, page.prevUrl(), TASKID);
		Issue issue = new Issue();
		issue.setIssue(issueName);
		issue.setYear(pubYear);
		issue.setTaskId(TASKID);
		issue.setWebsite(page.url());
		issue.setJournalId(journalid);
		IssueDao.addIssue(issue);
	}

	private void parseJournal(Page page) {
		String fullChineseName = "放射学实践";
		String fullEnglishName = "Radiologic Practice";
		String issnPrint = "1000-0313";
		
		page.put("fullEnglishName", fullEnglishName);
		page.put("fullChineseName", fullChineseName);
		page.put("shortEnglishName", "");
		page.put("issnPrint", issnPrint);
		page.put("issnOnline", "");
		
		Journal journal = new Journal();
		journal.setWebsite(page.url());
		journal.setIssn(issnPrint);
		journal.setTaskId(TASKID);
		journal.setJournalname(fullChineseName + "<br>" + fullEnglishName);
		JournalDao.addJournal(journal);
	}

	public static void main(String[] args) {
		Fsxsj claw = new Fsxsj();
		claw.setBaseDirectory("E:/webSpider");
		claw.addSeed("http://www.fsxsj.net/ch/index.aspx");
		claw.addByHand();
		claw.addRegex("http://www.fsxsj.net/ch/reader/view_abstract.aspx.file_no=\\d+.flag=1", "paper");
		
		claw.start();
		claw.asXml();
		
	}

	private void asXml() {
		for (int i = 0; i < list.size(); i+=50) {
			int toIndex = i + 50 > list.size() ? list.size() : i + 50;
			List<Attachment> subList = list.subList(i, toIndex);
			XmlUtil.generateXml(subList, new File(this.getConfig().getBaseDirectory()+"/xml", getClass().getSimpleName()+i+".xml"));
		}
	}

}
